package com.music.service.playlists.beans;

import java.util.Date;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.music.service.playlists.constants.PlaylistState;
import com.music.service.songs.beans.Song;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Playlist {

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long playlistId;

	@Persistent
	private String playlistName;
	@Persistent
	private Long memberId;
	@Persistent
	private int state;
	@Persistent
	private Long creationDate;
	@Persistent
	private Date lastUpdateDate;
	@Persistent
	private Date lastAccessedDate;
	@Persistent
	private boolean repeat;
	@Persistent
	private boolean shuffle;
	@Persistent
	private Long lastAddedSongId;

	/**
	 * We are using Set because songid's should be unique. 
	 */
	@Persistent
	private Set<Long> playlistSongs;
	
	@NotPersistent
	private Map<Long, Song> mSongsMap;

	public Playlist() {
		mSongsMap = new HashMap<Long, Song>();
		repeat = false;
		shuffle = false;
		playlistSongs = new TreeSet<Long>();
		state = PlaylistState.ACTIVE.getState();
	}

	
	/**
	 * @return the playlistId
	 */
	public Long getPlaylistId() {
		return playlistId;
	}

	/**
	 * @return the playlistSongs
	 */
	public Set<Long> getPlaylistSongs() {
		return playlistSongs;
	}

	/**
	 * @param pPlaylistSongs the playlistSongs to set
	 */
	public void setPlaylistSongs(Set<Long> pPlaylistSongs) {
		playlistSongs = pPlaylistSongs;
	}

	/**
	 * @param pPlaylistId
	 *            the playlistId to set
	 */
	public void setPlaylistId(Long pPlaylistId) {
		playlistId = pPlaylistId;
	}

	/**
	 * @return the playlistName
	 */
	public String getPlaylistName() {
		return playlistName;
	}

	/**
	 * @param pPlaylistName
	 *            the playlistName to set
	 */
	public void setPlaylistName(String pPlaylistName) {
		playlistName = pPlaylistName;
	}

	
	/**
	 * @return the memberId
	 */
	public Long getMemberId() {
		return memberId;
	}

	/**
	 * @param pMemberId the memberId to set
	 */
	public void setMemberId(Long pMemberId) {
		memberId = pMemberId;
	}

	/**
	 * @return the playlistState
	 */
	public PlaylistState getPlaylistState() {
		return PlaylistState.getPlaylistState(state);
	}

	/**
	 * @param pPlaylistState
	 *            the playlistState to set
	 */
	public void setPlaylistState(PlaylistState pPlaylistState) {
		state = pPlaylistState.getState();
	}

	/**
	 * @return the creationDate
	 */
	public Date getCreationDate() {
		if (creationDate != null) {
			Date date = new Date(creationDate);
			return date;
		}
		return null;
	}

	/**
	 * @param pCreationDate
	 *            the creationDate to set
	 */
	public void setCreationDate(Date pCreationDate) {
		creationDate = pCreationDate.getTime();
	}

	/**
	 * @return the lastAddedSongId
	 */
	public Long getLastAddedSongId() {
		return lastAddedSongId;
	}

	/**
	 * @param pLastAddedSongId
	 *            the lastAddedSongId to set
	 */
	public void setLastAddedSongId(Long pLastAddedSongId) {
		lastAddedSongId = pLastAddedSongId;
	}

	/**
	 * @return the lastUpdateDate
	 */
	public Date getLastUpdateDate() {
		return lastUpdateDate;
	}

	/**
	 * @param pLastUpdateDate
	 *            the lastUpdateDate to set
	 */
	public void setLastUpdateDate(Date pLastUpdateDate) {
		lastUpdateDate = pLastUpdateDate;
	}

	/**
	 * @return the songsMap
	 */
	public Map<Long, Song> getSongsMap() {
		return mSongsMap;
	}

	/**
	 * @param pSongsMap
	 *            the songsMap to set
	 */
	public void setSongsMap(Map<Long, Song> pSongsMap) {
		mSongsMap = pSongsMap;
	}

	public void addSong(Song pSong) {
		if (getSongsMap() == null) {
			setSongsMap(new HashMap<Long, Song>());
		}
		getSongsMap().put(pSong.getSongId(), pSong);
	}

	public Collection<Song> getSongs() {
		if (getSongsMap() != null) {
			return getSongsMap().values();
		}
		// return empty array.
		return new ArrayList<Song>();
	}

	public Set<Long> getSongIds() {
		if (getSongsMap() != null) {
			return getSongsMap().keySet();
		}
		// return empty set.
		return new HashSet<Long>();
	}

	/**
	 * @return the lastAccessedDate
	 */
	public Date getLastAccessedDate() {
		return lastAccessedDate;
	}

	/**
	 * @param pLastAccessedDate
	 *            the lastAccessedDate to set
	 */
	public void setLastAccessedDate(Date pLastAccessedDate) {
		lastAccessedDate = pLastAccessedDate;
	}

	/**
	 * @return the repeat
	 */
	public boolean isRepeat() {
		return repeat;
	}

	/**
	 * @param pRepeat
	 *            the repeat to set
	 */
	public void setRepeat(boolean pRepeat) {
		repeat = pRepeat;
	}

	/**
	 * @return the shuffle
	 */
	public boolean isShuffle() {
		return shuffle;
	}

	/**
	 * @param pShuffle
	 *            the shuffle to set
	 */
	public void setShuffle(boolean pShuffle) {
		shuffle = pShuffle;
	}

	public String toString(){
		StringBuilder sb = new StringBuilder();
		sb.append("pId="+getPlaylistId());
		sb.append("state="+getPlaylistState());
		sb.append("name="+getPlaylistName());
		return sb.toString();
		
	}
}
